"""
@file: 商品详情页.py
@author: yangsiting
@data: 2021/04/16
@desc:
"""
import re
import sqlite3
import requests
import random

uapools = [
'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; .NET4.0C; .NET4.0E; rv:11.0) like Gecko',
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299'
]

def get_url():
    dict1 = dict()
    conn = sqlite3.connect("data.sqlite")
    c = conn.cursor()
    cursor = c.execute("select goods_id,goods_link from goods_info;")
    for i in cursor:
        dict1[i[0]]=i[1]
    conn.close()
    return dict1


def get_data(url):
    try:
        thisua = random.choice(uapools)
        header = {"User-Agent": thisua}
        data = requests.get(url, headers=header)
        return data.text
    except:
        print('获取数据失败')

def deal_data(data):
    try:
        good_ms = re.findall('''<span class="head_title_name" title="(.*?)">''',data)
        res = good_ms[0].replace('&nbsp;', '')
        # good_price = re.match('''<p id="dd-price">(\s*)<span class="yen">&yen;</span>(\s*)(.*?)(\s*)</p>''',data)
        # good_price = good_price.group(1)
        return res
    except:
        print('异常')


def write_database(i,data):
    conn = sqlite3.connect("data.sqlite")
    cur = conn.cursor()
    goods_id = i
    goods_detail = data
    sql = "insert into goods_details(goods_id,goods_detail) values(%s,'%s')"%(goods_id,goods_detail)
    try:
        cur.execute(sql)
    except Exception as e:
        print(e)
    conn.commit()
    cur.close()
    conn.close()


def main():
        urls_info = get_url()
        for i in urls_info.keys():
            print(i)
            url = urls_info[i]
            data = get_data(url)
            good_detail = deal_data(data)
            write_database(i,good_detail)


if __name__ == '__main__':
    main()
    print('done')